<template>
  <el-dialog
    title="提示"
    :visible="dialogVisible"
    width="30%"
    @close="closeDilog"
  >
    <template #default>
      <el-form ref="refForm" :model="roleForm" :rules="rules" label-width="100px">
        <el-form-item label="角色名称" prop="name">
          <el-input v-model="roleForm.name" />
        </el-form-item>
        <el-form-item label="角色描述" prop="description">
          <el-input v-model="roleForm.description" />
        </el-form-item>
      </el-form>
    </template>

    <template #footer>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="confirm">确 定</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script>
import { fetchAddRole, fetchRoleDetail } from '@/api/role'
export default {
  name: 'AddRole',
  props: {
    dialogVisible: {
      type: Boolean
    }
  },
  data() {
    return {
      roleForm: {
        name: '',
        description: ''
      },
      rules: {
        name: [{ required: true, message: '角色名称不能为空', trigger: 'blur' }],
        description: [{ required: true, message: '角色描述不能为空', trigger: 'blur' }]
      }
    }
  },
  methods: {
    closeDilog() {
      this.$emit('close-dialog')
    },
    confirm() {
      this.$refs.refForm.validate(async valid => {
        if (valid) {
          await fetchAddRole(this.roleForm)
          this.$message.success('添加成功')
          this.$emit('update-list')
          this.resetForm()
        }
      })
    },
    resetForm() {
      this.closeDilog()
      this.$refs.refForm.resetFields()
      this.roleForm = {
        name: '',
        description: ''
      }
    },
    cancel() {
      this.resetForm()
    },
    async getRoleDetail(id) {
      const res = await fetchRoleDetail(id)
      this.roleForm = res
    }
  }
}
</script>

<style>
</style>
